home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Tools&Utilities / TouchMe 1.2□ / touchMe 1.2 Folder / touchMe source codes / CW11 PP source / source / CTouchMeAppleEvents.cp < prev    next >
Encoding:
Text File  |  1997-04-25  |  14.6 KB  |  524 lines  |  [TEXT/CWIE]

  1. // ==================================================
  2. //    CTouchMeAppleEvents.cp
  3. //    a part of CTouchMeApp.cp
  4. //    Copyright (C) 1996-1997 Mizutori Tetsuya
  5. //    July 4, 1996; February 3, 1997; April 24, 1997.
  6. // ==================================================
  7. //    All documents are pretty-printed in 10-point Geneva font.
  8.  
  9.  
  10. #include <UAppleEventsMgr.h>
  11. #include <UExtractFromAEDesc.h>
  12.  
  13. #include "touchMeConstants.h"
  14. #include "CTouchMeAppleEvents.h"
  15. #include "CTouchMeApp.h"
  16. #include "CTouchMePref.h"
  17. #include "CTouchMeMainWindow.h"
  18.  
  19. #include "UAppleEvents.h"
  20. #include "UFileTools.h"
  21. #include "UMacOSTools.h"
  22.  
  23.  
  24. #define longDateTimeHi( a )    (((LongDateCvt *) &(a))->hl.lHigh)
  25. #define longDateTimeLo( a )    (((LongDateCvt *) &(a))->hl.lLow)
  26.  
  27.  
  28. // --------------------------------------------------
  29. //        • HandleAppleEvent
  30. // --------------------------------------------------
  31. //    Respond to an AppleEvent
  32.  
  33. void
  34. CTouchMeApp::HandleAppleEvent(
  35.     const AppleEvent    &    inAppleEvent,
  36.     AppleEvent    &        outAEReply,
  37.     AEDesc &            outResult,
  38.     long                inAENumber )
  39. {
  40.  
  41.     switch ( inAENumber ) {
  42.         case ae_GetPrefs:
  43.             HandleGetPrefs( inAppleEvent, outAEReply, outResult );
  44.             break;
  45.  
  46.         case ae_SetPrefs:
  47.             HandleSetPrefs( inAppleEvent, outAEReply, outResult );
  48.             break;
  49.  
  50.         case ae_LoadPrefs:
  51.             HandleLoadPrefs( inAppleEvent, outAEReply, outResult );
  52.             break;
  53.  
  54.         case ae_SavePrefs:
  55.             HandleSavePrefs( inAppleEvent, outAEReply, outResult );
  56.             break;
  57.  
  58.         case ae_Touch:
  59.             HandleTouch( inAppleEvent, outAEReply, outResult );
  60.             break;
  61.  
  62.         case ae_Fetch:
  63.             HandleFetch( inAppleEvent, outAEReply, outResult );
  64.             break;
  65.  
  66.         default:
  67.             LDocApplication::HandleAppleEvent(
  68.                         inAppleEvent, outAEReply, outResult, inAENumber );
  69.             break;
  70.     }
  71. }
  72.  
  73.  
  74. // --------------------------------------------------
  75. //        • HandleGetPrefs
  76. // --------------------------------------------------
  77.  
  78. void
  79. CTouchMeApp::HandleGetPrefs(
  80.     const AppleEvent &    inAppleEvent,
  81.     AppleEvent &        outAEReply,
  82.     AEDesc &            outResult )
  83. {
  84. #pragma unused ( outAEReply )
  85.  
  86.     // Get the 1st required parameter of 'keyDirectObject' as 'typeEnumerated'.
  87.     OSType        theEnumValue;
  88.     UAppleEvents::GetParamEnum( inAppleEvent, keyDirectObject, theEnumValue );
  89.  
  90.     // Error if there are more parameters.
  91.     UAppleEventsMgr::CheckForMissedParams( inAppleEvent );
  92.  
  93.     ETouchType    theTouchType;
  94.     switch ( (EnumTouchMe_Type) theEnumValue ) {
  95.         case kEnumType_Creation:
  96.             theTouchType = touchType_CreationDate;
  97.             break;
  98.         case kEnumType_Modification:
  99.             theTouchType = touchType_ModificationDate;
  100.             break;
  101.         default:
  102.             ThrowIfOSErr_( errAEWrongDataType );
  103.             break;
  104.     }
  105.  
  106.     HandleGetPrefsSettings( outResult, theTouchType );
  107.  
  108. }
  109.  
  110.  
  111. // --------------------------------------------------
  112. //        • HandleSetPrefs
  113. // --------------------------------------------------
  114.  
  115. void
  116. CTouchMeApp::HandleSetPrefs(
  117.     const AppleEvent &    inAppleEvent,
  118.     AppleEvent &        outAEReply,
  119.     AEDesc &            outResult )
  120. {
  121. #pragma unused ( outAEReply, outResult )
  122.  
  123.  
  124.     // Get the 1st required parameter of 'keyDirectObject' as 'typeEnumerated'.
  125.     OSType        theEnumValue;
  126.     UAppleEvents::GetParamEnum( inAppleEvent, keyDirectObject, theEnumValue );
  127.  
  128.     // Get the 2nd required parameter of 'keyAEData' as 'typeWildCard'.
  129.     StAEDescriptor    theDescData;
  130. //    theDescData.GetParamDesc( inAppleEvent, keyAEData, typeAERecord );
  131.     theDescData.GetParamDesc( inAppleEvent, keyAEData, typeWildCard );
  132.  
  133.     // Error if there are more parameters.
  134.     UAppleEventsMgr::CheckForMissedParams( inAppleEvent );
  135.  
  136.     ETouchType    theTouchType;
  137.     switch ( (EnumTouchMe_Type) theEnumValue ) {
  138.         case kEnumType_Creation:
  139.             theTouchType = touchType_CreationDate;
  140.             break;
  141.         case kEnumType_Modification:
  142.             theTouchType = touchType_ModificationDate;
  143.             break;
  144.         default:
  145.             ThrowIfOSErr_( errAEWrongDataType );
  146.             break;
  147.     }
  148.  
  149.     HandleSetPrefsSettings( theDescData.mDesc, theTouchType );
  150.  
  151. }
  152.  
  153.  
  154. // --------------------------------------------------
  155. //        • HandleLoadPrefs
  156. // --------------------------------------------------
  157.  
  158. void
  159. CTouchMeApp::HandleLoadPrefs(
  160.     const AppleEvent &    inAppleEvent,
  161.     AppleEvent &        outAEReply,
  162.     AEDesc &            outResult )
  163. {
  164. #pragma unused ( inAppleEvent, outAEReply, outResult )
  165.  
  166.     mPref->LoadPrefsData();
  167.  
  168.     // If dialog window is open, then setup the dialog according to the preferences.
  169.     if ( mMainWindow != NULL )
  170.         mPref->SetPrefsToWindow( mMainWindow );
  171. }
  172.  
  173.  
  174. // --------------------------------------------------
  175. //        • HandleSavePrefs
  176. // --------------------------------------------------
  177.  
  178. void
  179. CTouchMeApp::HandleSavePrefs(
  180.     const AppleEvent &    inAppleEvent,
  181.     AppleEvent &        outAEReply,
  182.     AEDesc &            outResult )
  183. {
  184. #pragma unused ( inAppleEvent, outAEReply, outResult )
  185.  
  186.     // If the main window is open, then read the settings from the dialog.
  187.     if ( mMainWindow != NULL )
  188.         mPref->GetPrefsFromWindow( mMainWindow );
  189.  
  190.     mPref->SavePrefsData();
  191. }
  192.  
  193.  
  194. // --------------------------------------------------
  195. //        • HandleTouch
  196. // --------------------------------------------------
  197.  
  198. void
  199. CTouchMeApp::HandleTouch(
  200.     const AppleEvent &    inAppleEvent,
  201.     AppleEvent &         outAEReply,
  202.     AEDesc &            outResult )
  203. {
  204. #pragma unused ( outAEReply, outResult )
  205.  
  206.     OSErr        err;
  207.  
  208.     // Get the 1st required parameter as 'typeWildCard'.
  209. //    Get the 1st required parameter as 'typeAEList'.
  210. //    StAEDescriptor    theDocListDesc;
  211. //    theDocListDesc.GetParamDesc( inAppleEvent, keyDirectObject, typeAEList );
  212.     StAEDescriptor        theRequiredDesc;
  213.     theRequiredDesc.GetParamDesc( inAppleEvent, keyDirectObject, typeWildCard );
  214.     DescType            theDirectObjectType;
  215.     theDirectObjectType = theRequiredDesc.mDesc.descriptorType;
  216.     switch ( theDirectObjectType ) {
  217.         case typeAEList:
  218.         case typeAlias:
  219.             break;
  220.         default:
  221.             ThrowOSErr_( errAECoercionFail );
  222.             break;
  223.     }
  224.  
  225.     // Error if there are more parameters.
  226.     UAppleEventsMgr::CheckForMissedParams( inAppleEvent );
  227.  
  228.  
  229.     switch ( theDirectObjectType ) {
  230.  
  231.         // If the direct object is a list of files, then touch each file.
  232.         case typeAEList:
  233.         {
  234.             long    theCount;
  235.             err = ::AECountItems( &(theRequiredDesc.mDesc), &theCount );
  236.             ThrowIfOSErr_(err);
  237.  
  238.             for ( long index = 1; index <= theCount; index++ ) {
  239.                 AEKeyword        theKey;
  240.                 DescType        theType;
  241.                 FSSpec        theFSSpec;
  242.                 Size            theSize;
  243.                 err = ::AEGetNthPtr( &(theRequiredDesc.mDesc), index, typeFSS,
  244.                         &theKey, &theType, (Ptr) &theFSSpec, sizeof(FSSpec), &theSize );
  245.                 ThrowIfOSErr_(err);
  246.  
  247.                 OpenDocument( &theFSSpec );
  248.             }
  249.         }
  250.             break;
  251.  
  252.         // If the direct object is a single files, then touch it.
  253.         case typeAlias:
  254.         {
  255.             FSSpec    theFSSpec;
  256.             UAppleEvents::TheFSSpec( theRequiredDesc.mDesc, theFSSpec );
  257.  
  258.             OpenDocument( &theFSSpec );
  259.         }
  260.             break;
  261.     }
  262.  
  263. }
  264.  
  265.  
  266. // --------------------------------------------------
  267. //        • HandleFetch
  268. // --------------------------------------------------
  269.  
  270. void
  271. CTouchMeApp::HandleFetch(
  272.     const AppleEvent &    inAppleEvent,
  273.     AppleEvent &        outAEReply,
  274.     AEDesc &            outResult )
  275. {
  276. #pragma unused ( outAEReply )
  277.  
  278.     OSErr        err;
  279.  
  280. //    unsigned long    theCreationDate, theModificationDate;
  281. //    ::GetDateTime( &theCreationDate );
  282. //    ::GetDateTime( &theModificationDate );
  283.  
  284.     // Get the 1st required parameter as 'typeWildCard'.
  285. //    Get the 1st required parameter of 'keyDirectObject' as 'typeAEList'.
  286. //    StAEDescriptor    theDocListDesc;
  287. //    theDocListDesc.GetParamDesc( inAppleEvent, keyDirectObject, typeAEList );
  288.     StAEDescriptor        theRequiredDesc;
  289.     theRequiredDesc.GetParamDesc( inAppleEvent, keyDirectObject, typeWildCard );
  290.     DescType            theDirectObjectType;
  291.     theDirectObjectType = theRequiredDesc.mDesc.descriptorType;
  292.     switch ( theDirectObjectType ) {
  293.         case typeAEList:
  294.         case typeAlias:
  295.             break;
  296.         default:
  297.             ThrowOSErr_( errAECoercionFail );
  298.             break;
  299.     }
  300.  
  301.     // Error if there are more parameters.
  302.     UAppleEventsMgr::CheckForMissedParams( inAppleEvent );
  303.  
  304.  
  305.     switch ( theDirectObjectType ) {
  306.  
  307.         // If the direct object is a list of files, then fetch the date time stamp of them.
  308.         case typeAEList:
  309.         {
  310.             long    theCount;
  311.             err = ::AECountItems( &(theRequiredDesc.mDesc), &theCount );
  312.             ThrowIfOSErr_(err);
  313.  
  314.             // The first item is significant. The rest ones will be ignored.
  315.             for ( long index = 1; index <= 1 /*theCount*/; index++ ) {
  316.                 AEKeyword        theKey;
  317.                 DescType        theType;
  318.                 FSSpec        theFSSpec;
  319.                 Size            theSize;
  320.                 err = ::AEGetNthPtr( &(theRequiredDesc.mDesc), index, typeFSS,
  321.                         &theKey, &theType, (Ptr) &theFSSpec, sizeof(FSSpec), &theSize );
  322.                 ThrowIfOSErr_(err);
  323.  
  324.             //    UFileTools::GetFSSpecDateTime( theFSSpec, theCreationDate, theModificationDate );
  325.                 HandleFetchDateTime( outResult, theFSSpec );
  326.             }
  327.         }
  328.             break;
  329.  
  330.         // If the direct object is a single files, then fetch the date time stamp of it.
  331.         case typeAlias:
  332.         {
  333.             FSSpec    theFSSpec;
  334.             UAppleEvents::TheFSSpec( theRequiredDesc.mDesc, theFSSpec );
  335.  
  336.         //    UFileTools::GetFSSpecDateTime( theFSSpec, theCreationDate, theModificationDate );
  337.             HandleFetchDateTime( outResult, theFSSpec );
  338.  
  339.         }
  340.             break;
  341.     }
  342.  
  343. }
  344.  
  345.  
  346.  
  347. // ==================================================
  348. //    Common functions
  349. // ==================================================
  350.  
  351. // --------------------------------------------------
  352. //        • HandleGetPrefsSettings
  353. // --------------------------------------------------
  354.  
  355. void
  356. CTouchMeApp::HandleGetPrefsSettings(
  357.     AERecord &        ioAERecord,
  358.     ETouchType        inTouchType,
  359.     Boolean            toCreateNewAERecord )    // true to dispose desc and create a new record [true]
  360. {
  361.     OSErr    err;
  362.  
  363.     // Dispose the original descriptor given by the system.
  364.     // Instead, create my own descriptor, a record type with true option, otherwise a list type.
  365.     if ( toCreateNewAERecord ) {
  366.         ::AEDisposeDesc( &ioAERecord );
  367.         ioAERecord.dataHandle = NULL;
  368.         err = ::AECreateList( NULL, 0, true, &ioAERecord );    // Create as 'record'.
  369.         ThrowIfOSErr_( err );
  370.     }
  371.  
  372.     // If the main window is open, then read the settings from the dialog.
  373.     if ( mMainWindow != NULL )
  374.         mPref->GetPrefsFromWindow( mMainWindow );
  375.  
  376.     // Return the mPref data as an AppleEvent record.
  377.     // 1. Enabled
  378.         Boolean        theEnabled = mPref->GetEnabled( inTouchType );
  379.  
  380.     // 2. Flag
  381.         OSType        theFlag = typeNull;
  382.         switch ( (ETouchFlag) mPref->GetFlagNumb( inTouchType ) ) {
  383.             case touchFlag_Current:    theFlag = kEnumFlag_Current; break;
  384.             case touchFlag_Direct:        theFlag = kEnumFlag_Excact; break;
  385.             case touchFlag_First:        theFlag = kEnumFlag_First; break;
  386.             case touchFlag_Second:        theFlag = kEnumFlag_FirstSet; break;
  387.             default:    break;
  388.         }
  389.  
  390.     // 3. Value
  391.         LongDateTime    theDateTimeSecs;
  392.         longDateTimeHi( theDateTimeSecs ) = 0;
  393.         longDateTimeLo( theDateTimeSecs ) = mPref->GetDateTime( inTouchType );
  394.  
  395.  
  396.     // Prepare to create members of descriptor record.
  397.     StAEDescriptor        theDescEnabled( (Boolean) theEnabled );
  398.     StAEDescriptor        theDescFlag( typeEnumerated, &theFlag, sizeof(OSType) );
  399.     StAEDescriptor        theDescDate( typeLongDateTime, &theDateTimeSecs, sizeof(LongDateTime) );
  400.  
  401.     // Insert the members into to the descriptor record.
  402.     UAEDesc::AddKeyDesc( &ioAERecord, pPref_Enabled, theDescEnabled.mDesc );
  403.     UAEDesc::AddKeyDesc( &ioAERecord, pPref_Flag, theDescFlag.mDesc );
  404.     UAEDesc::AddKeyDesc( &ioAERecord, pPref_Value, theDescDate.mDesc );
  405. }
  406.  
  407.  
  408. // --------------------------------------------------
  409. //        • HandleSetPrefsSettings
  410. // --------------------------------------------------
  411.  
  412. void
  413. CTouchMeApp::HandleSetPrefsSettings(
  414.     AERecord &        inAERecord,
  415.     ETouchType        inTouchType )
  416. {
  417.     Boolean    parameterWasFound = false;
  418.  
  419.     // 1. Enabled (Get the optional parameter 'pPref_Enabled' as 'typeBoolean')
  420.     {
  421.         Boolean    theEnabled = mPref->GetEnabled( inTouchType );
  422.         Boolean    theNewEnabled;
  423.  
  424.         if ( UAppleEvents::GetParamBoolean( inAERecord, pPref_Enabled, theNewEnabled ) ) {
  425.             if ( theEnabled != theNewEnabled ) {
  426.                 mPref->SetEnabled( inTouchType, theNewEnabled );
  427.                 parameterWasFound = true;
  428.             }
  429.         }
  430.     }
  431.  
  432.  
  433.     // 2. Flag (Get the optional parameter 'pPref_Flag' as 'typeEnumerated')
  434.     {
  435.         ETouchFlag        theFlag = mPref->GetFlagNumb( inTouchType );
  436.         OSType        theAEFlag = typeNull;
  437.  
  438.         if ( UAppleEvents::GetParamEnum( inAERecord, pPref_Flag, theAEFlag ) ) {
  439.             ETouchFlag        theNewFlag = touchFlag_Null;
  440.             switch ( (EnumTouchMe_Flag) theAEFlag ) {
  441.                 case kEnumFlag_Current:    theNewFlag = touchFlag_Current; break;
  442.                 case kEnumFlag_Excact:    theNewFlag = touchFlag_Direct; break;
  443.                 case kEnumFlag_First:        theNewFlag = touchFlag_First; break;
  444.                 case kEnumFlag_FirstSet:    theNewFlag = touchFlag_Second; break;
  445.                 default:    break;
  446.             }
  447.  
  448.             if ( theFlag != theNewFlag && theNewFlag != touchFlag_Null ) {
  449.                 mPref->SetFlagNumb( inTouchType, theNewFlag );
  450.                 parameterWasFound = true;
  451.             }
  452.  
  453.         }
  454.     }
  455.  
  456.     // 3. Value (Get the optional parameter 'pPref_Value' as 'typeBoolean')
  457.     {
  458.         LongDateTime    theDateTimeSecs;
  459.         longDateTimeHi( theDateTimeSecs ) = 0;
  460.         longDateTimeLo( theDateTimeSecs ) = mPref->GetDateTime( inTouchType );
  461.         LongDateTime    theNewDateTimeSecs;
  462.         if ( UAppleEvents::GetParamLongDateTime( inAERecord, pPref_Value, theNewDateTimeSecs ) ) {
  463.             if ( longDateTimeHi(theDateTimeSecs) != longDateTimeHi(theNewDateTimeSecs) ||
  464.                 longDateTimeLo(theDateTimeSecs) != longDateTimeLo(theNewDateTimeSecs) ) {
  465.                 mPref->SetDateTime( inTouchType, longDateTimeLo( theNewDateTimeSecs ) );
  466.                 parameterWasFound = true;
  467.             }
  468.         }
  469.     }
  470.  
  471.     if ( parameterWasFound && mMainWindow != NULL )
  472.         mPref->SetPrefsToWindow( mMainWindow );
  473.  
  474. }
  475.  
  476.  
  477. // --------------------------------------------------
  478. //        • HandleFetchDateTime
  479. // --------------------------------------------------
  480.  
  481. void
  482. CTouchMeApp::HandleFetchDateTime(
  483.     AERecord &        ioAERecord,
  484.     FSSpec &            inMacFSSpec,
  485.     Boolean            toCreateNewAERecord )    //    = true
  486. {
  487.     OSErr    err;
  488.  
  489.     // Dispose the original descriptor given by the system.
  490.     // Instead, create my own descriptor, a record type with true option, otherwise a list type.
  491.     if ( toCreateNewAERecord ) {
  492.         ::AEDisposeDesc( &ioAERecord );
  493.         ioAERecord.dataHandle = NULL;
  494.         err = ::AECreateList( NULL, 0, true, &ioAERecord );    // Create as 'record'.
  495.         ThrowIfOSErr_( err );
  496.     }
  497.  
  498.     unsigned long    theCreationDate, theModificationDate;
  499.     UFileTools::GetFSSpecDateTime( inMacFSSpec, theCreationDate, theModificationDate );
  500.  
  501.     mPref->SetDateTime( touchType_CreationDate, theCreationDate );
  502.     mPref->SetDateTime( touchType_ModificationDate, theModificationDate );
  503.  
  504.     // If the main window is open, then setup the dialog according to the preferences.
  505.     if ( mMainWindow != NULL ) mPref->SetPrefsToWindow( mMainWindow );
  506.  
  507.     LongDateTime    theCrDateTimeSecs, theMdDateTimeSecs;
  508.     longDateTimeHi( theCrDateTimeSecs ) = 0;
  509.     longDateTimeLo( theCrDateTimeSecs ) = theCreationDate;
  510.     longDateTimeHi( theMdDateTimeSecs ) = 0;
  511.     longDateTimeLo( theMdDateTimeSecs ) = theModificationDate;
  512.  
  513.     // Prepare to create members of descriptor record.
  514.     StAEDescriptor        theCrDateDesc( typeLongDateTime, &theCrDateTimeSecs, sizeof(LongDateTime) );
  515.     StAEDescriptor        theMdDateDesc( typeLongDateTime, &theMdDateTimeSecs, sizeof(LongDateTime) );
  516.  
  517.     // Insert the members into to the descriptor record.
  518.     UAEDesc::AddKeyDesc( &ioAERecord, pStamp_Creation, theCrDateDesc.mDesc );
  519.     UAEDesc::AddKeyDesc( &ioAERecord, pStamp_Modification, theMdDateDesc.mDesc );
  520. }
  521.  
  522.  
  523. // end of program
  524.